package 
{
	import asf.core.Application;
	import asf.events.ApplicationEvent;
	import asf.events.ApplicationLoadProgressEvent;
	import asf.log.LogLevel;
	import asf.utils.Align;
	
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.netoleal.utils.TraceUtils;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	import sample.app.Globals;
	
	import sample.components.Preloader;
	
	public class Framework_ASF_Test extends Sprite
	{
		private var app:Application;
		private var container:Sprite;
		
		private var preloader:Preloader;
		
		public function Framework_ASF_Test( )
		{
			
			container = new Sprite( );
			addChild( container );
			
			SWFAddress.onInit = swfAddressOnInt;
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			app = new Application( container, root.loaderInfo, Globals.APP_ID );
			
			app.setLogLevel( LogLevel.INFO_3 );
			
			app.addEventListener( ApplicationEvent.CONFIG_FILE_LOADED, appOnConfigLoaded );
			app.addEventListener( ApplicationEvent.AFTER_CONFIG_FILE_LOADED, appAfterConfigLoaded );
			
			app.addEventListener( ApplicationLoadProgressEvent.DEPENDENCIES_LOAD_COMPLETE, appOnComplete );
			app.addEventListener( ApplicationLoadProgressEvent.DEPENDENCIES_LOAD_START, appOnLoadStart );
			app.addEventListener( BulkProgressEvent.PROGRESS, appOnLoadProgress );
			
			app.parameters.defaults.basepath = "../";
			app.parameters.defaults.config = "{basepath}xml/application.xml";
			
			log( app.parameters.config );
			
			preloader = new Preloader( );
			Align.add( preloader, Align.MIDDLE + Align.CENTER, { margin_top: - 200 } );
			
			addChild( preloader );
			
			preloader.show( ).queue( app.load, app.parameters.config );
		}
		
		private function appOnLoadProgress( evt:BulkProgressEvent ):void
		{
			preloader.setProgress( evt.percentLoaded ); 
		}
		
		private function swfAddressOnInt( ):void
		{
			log( );
		}
		
		private function appOnConfigLoaded( evt:ApplicationEvent ):void
		{
			log( app.parameters[ "localedir" ] );
			log( app.parameters.test1 );
			log( app.parameters.test2 );
			log( app.parameters.testeFlashVars );
			log( app.parameters.debug );
			log( app.parameters.variableValue );
			
			log( app.layers.background );
			log( TraceUtils.getFullPath( app.layers.sections ) );
			log( app.navigation.sections );
		}
		
		private function appAfterConfigLoaded( evt:ApplicationEvent ):void
		{
			log( );
			
			//app.pauseLoading( );
			
			//Delay.call( 5000, app.resumeLoading );
		}
		
		private function appOnLoadStart( evt:ApplicationLoadProgressEvent ):void
		{
			log( );
		}
		
		private function appOnComplete( evt:Event ):void
		{
			log( );
			
			dtrace( app.styles.styleNames );
			dtrace( app.getStyleByID( "style2" ).getStyle( "embedTest" ) );
			
			preloader.hide( ).queue( initSite );
		}
		
		private function initSite( ):void
		{
			var main:Object = app.dependencies.getContent( "main" );
			
			Align.remove( preloader );
			removeChild( preloader );
			preloader = null;
			
			try
			{
				main.init( );
			}
			catch( e:Error )
			{
				if( main.hasOwnProperty( "init" ) )
				{
					log( e );
				}
				else
				{
					app.log( "WARNING! There is no method init on Main", LogLevel.ERROR_1 );
				}
			}
		}
	}
}
